package com.gzx.spring.apis.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gzx.risk.core.apis.context.ApiContext;
import com.gzx.risk.core.apis.result.RiskResult;
import com.gzx.risk.core.apis.result.yuyao.YuyaoBrdt_score;
import com.gzx.risk.core.apis.result.yuyao.YuyaoGatewayC108139;
import com.gzx.risk.core.apis.result.yuyao.YuyaoGatewayC108143;
import com.gzx.risk.core.apis.result.yuyao.YuyaoGatewayC108144;
import com.gzx.risk.core.apis.result.yuyao.YuyaoMobileOnLineVerify;
import com.gzx.risk.core.apis.result.yuyao.YuyaoMobilestatus;
import com.gzx.risk.core.apis.result.yuyao.YuyaoPersonal;
import com.gzx.risk.core.apis.result.yuyao.YuyaoRa_high_score;
import com.gzx.spring.apis.utils.YuyaoUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class YuyaoService {
	@Value("${apis.yuyao.appId}")
	private String appId;
	@Value("${apis.yuyao.appSecret}")
	private String appSecret;
	@Value("${apis.yuyao.apiUrl}")
	private String apiUrl;
	@Value("${apis.yuyao.env}")
	private String env;

	private final ObjectMapper objectMapper;

	public YuyaoService(ObjectMapper objectMapper) {
		this.objectMapper = objectMapper;
	}

	public String ra_high_score(String name, String id, String phone) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.ra_high_score(name, id, phone);
	}

	public String brdt_score(String name, String id, String phone) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.brdt_score(name, id, phone);
	}

	public String gatewayC108139(String phone) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.gatewayC108139(phone);
	}

	public String gatewayC108144(String name, String id, String phone) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.gatewayC108144(name, id, phone);
	}

	public String mobileOnLineVerify(String phone) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.mobileOnLineVerify(phone);
	}

	public String mobilestatus(String phone) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.mobilestatus(phone);
	}

	public String gatewayC108143(String name, String id, String phone) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.gatewayC108143(name, id, phone);
	}

	public String personal(String name, String id, String authDate) throws Exception {
		YuyaoUtil util = new YuyaoUtil().apiUrl(apiUrl).appId(appId).appSecret(appSecret);
		return util.personal(name, id, authDate);
	}

	// ,"data":[]---->""
	private static String convert(String json) {
		return json.replaceAll(",\"data\":\\[\\]", "");
	}

	public RiskResult ra_high_score(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_ra_high_score";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/ra_high_score");
		} else {
			json = this.ra_high_score(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(),
					ctx.getUserInfo().getPhone());
			log.debug("{}:{}", apiName, json);
		}
		json = convert(json);
		YuyaoRa_high_score obj = objectMapper.readValue(json, YuyaoRa_high_score.class);
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

	public RiskResult brdt_score(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_brdt_score";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/brdt_score");
		} else {
			json = this.brdt_score(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(),
					ctx.getUserInfo().getPhone());
			log.debug("{}:{}", apiName, json);
		}

		json = convert(json);
		YuyaoBrdt_score obj = objectMapper.readValue(json, YuyaoBrdt_score.class);
		RiskResult.trim(obj.getData());
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

	public RiskResult gatewayC108139(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_gatewayC108139";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/gatewayC108139");
		} else {
			json = this.gatewayC108139(ctx.getUserInfo().getPhone());
			log.debug("{}:{}", apiName, json);
		}
		json = convert(json);
		YuyaoGatewayC108139 obj = objectMapper.readValue(json, YuyaoGatewayC108139.class);
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

	public RiskResult gatewayC108144(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_gatewayC108144";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/gatewayC108144");
		} else {
			json = this.gatewayC108144(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(),
					ctx.getUserInfo().getPhone());
			log.debug("{}:{}", apiName, json);
		}
		json = convert(json);
		YuyaoGatewayC108144 obj = objectMapper.readValue(json, YuyaoGatewayC108144.class);
		if (obj.getData() != null) {
			RiskResult.trim(obj.getData().getInfo());
		}
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

	public RiskResult mobileOnLineVerify(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_mobileOnLineVerify";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/mobileOnLineVerify");
		} else {
			json = this.mobileOnLineVerify(ctx.getUserInfo().getPhone());
			log.debug("{}:{}", apiName, json);
		}
		json = convert(json);
		YuyaoMobileOnLineVerify obj = objectMapper.readValue(json, YuyaoMobileOnLineVerify.class);
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

	public RiskResult mobilestatus(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_mobilestatus";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/mobilestatus");
		} else {
			json = this.mobilestatus(ctx.getUserInfo().getPhone());
			log.debug("{}:{}", apiName, json);
		}
		json = convert(json);
		YuyaoMobilestatus obj = objectMapper.readValue(json, YuyaoMobilestatus.class);
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		ctx.add(obj);
		log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		return obj;
	}

	public RiskResult gatewayC108143(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_gatewayC108143";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/gatewayC108143");
		} else {
			json = this.gatewayC108143(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(),
					ctx.getUserInfo().getPhone());
			log.debug("{}:{}", apiName, json);
		}
		json = convert(json);
		YuyaoGatewayC108143 obj = objectMapper.readValue(json, YuyaoGatewayC108143.class);
		RiskResult.trim(obj.getData());
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

	public RiskResult personal(String groupName, ApiContext ctx) throws Exception {
		String apiName = "yuyao_personal";
		String json = "";
		if ("debug".equals(env)) {
			json = ApiExec.loadJson("/debug/yuyao/personal");
		} else {
			json = this.personal(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(), ctx.getAuthDate());
			log.debug("{}:{}", apiName, json);
		}
		json = convert(json);
		YuyaoPersonal obj = objectMapper.readValue(json, YuyaoPersonal.class);
		if (obj.getData() != null) {
			RiskResult.trim(obj.getData().getInfo());
		}
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

}
